unit fMeshCube;

interface

uses
  Winapi.OpenGL,
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,
  Vcl.ExtCtrls,
  Vcl.Imaging.Jpeg,

  GLS.Texture,
  GLS.SceneViewer,
  GLS.Material,
  GLS.Scene,
  GLS.VectorTypes,
  GLS.RenderContextInfo,
  GLS.VectorFileObjects,
  GLS.File3DS,
  GLS.Objects,
  GLS.Cadencer,
  GLS.VectorLists,
  GLS.Keyboard,
  GLS.SimpleNavigation,
  GLS.Navigator,
  GLS.Coordinates,

  GLS.MeshBuilder,
  GLS.BaseClasses;

type
  TForm1 = class(TForm)
    GLScene1: TGLScene;
    GLSceneViewer1: TGLSceneViewer;
    MatLib: TGLMaterialLibrary;
    GLCamera1: TGLCamera;
    GLLightSource1: TGLLightSource;
    ffMesh: TGLFreeForm;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    scale_x: TEdit;
    scale_y: TEdit;
    scale_z: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    btnApply: TButton;
    OpenDialog1: TOpenDialog;
    GLCadencer1: TGLCadencer;
    GLNavigator1: TGLNavigator;
    GLUserInterface1: TGLUserInterface;
    cbMouseLook: TCheckBox;
    procedure GLDirectOpenGL1Render(var rci: TGLRenderContextInfo);
    procedure FormCreate(Sender: TObject);
    procedure btnApplyClick(Sender: TObject);
    procedure ButtonTexClick(Sender: TObject);
    procedure GLCadencer1Progress(Sender: TObject; const deltaTime, newTime: Double);
    procedure cbMouseLookClick(Sender: TObject);
  private
    FOldX, FOldY: Integer;
    MeshObject: TGLMeshObject;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Create Mesh Objects
  MeshObject := TGLMeshObject.CreateOwned(ffMesh.MeshObjects);
  MeshObject.Mode := momFaceGroups;

  MakeMeshHexahedron(MeshObject);
  ffMesh.StructureChanged;
end;

procedure TForm1.GLDirectOpenGL1Render(var rci: TGLRenderContextInfo);
begin
  glBegin(GL_QUADS);
  glVertex3f(-1.0, -1.0, 1.0);
  glVertex3f(1.0, -1.0, 1.0);
  glVertex3f(1.0, 1.0, 1.0);
  glVertex3f(-1.0, 1.0, 1.0);
  glEnd();
end;

procedure TForm1.GLCadencer1Progress(Sender: TObject; const deltaTime, newTime: Double);
begin
  if IsKeyDown('w') or IsKeyDown(VK_UP) then
    GLCamera1.Move(deltaTime);
  if IsKeyDown('a') or IsKeyDown(VK_RIGHT) then
    GLCamera1.Slide(-deltaTime);
  if IsKeyDown('s') or IsKeyDown(VK_DOWN) then
    GLCamera1.Move(-deltaTime);
  if IsKeyDown('d') or IsKeyDown(VK_LEFT) then
    GLCamera1.Slide(deltaTime);
  if IsKeyDown('z') then
    GLCamera1.Lift(deltaTime);
  if IsKeyDown('x') then
    GLCamera1.Lift(-deltaTime);
  if IsKeyDown(VK_ESCAPE) then
    Close;
  GLUserInterface1.MouseLook;
  GLUserInterface1.MouseUpdate;
end;

procedure TForm1.btnApplyClick(Sender: TObject);
begin
  ffMesh.Scale.SetVector(StrToFloat(scale_x.Text), StrToFloat(scale_y.Text),
    StrToFloat(scale_z.Text));
end;

procedure TForm1.cbMouseLookClick(Sender: TObject);
begin
  if cbMouseLook.Checked then
    GLUserInterface1.MouseLookActivate
  else
    GLUserInterface1.MouseLookDeactivate;
end;

procedure TForm1.ButtonTexClick(Sender: TObject);
begin
  SetCurrentDir(Application.ExeName);
  OpenDialog1.InitialDir := GetCurrentDir;
  if OpenDialog1.Execute then
  begin
    MatLib.Materials[TButton(Sender).Tag].Material.Texture.Disabled := true;
    MatLib.Materials[TButton(Sender).Tag].Material.Texture.Image.LoadFromFile(OpenDialog1.FileName);
    MatLib.Materials[TButton(Sender).Tag].Material.Texture.Disabled := false;

    ffMesh.StructureChanged;
  end;
end;

end.
